package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import ysq.xyg.model.ShareOrder;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.model.info.ShareOrderInfo;
import ysq.xyg.service.ItemService;
import ysq.xyg.service.ShareOrderService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;

/**
 * 分享订单
 *
 * @author congcong tao
 * @create 2018/8/26 下午10:12
 */
@Controller
@RequestMapping("/shareOrder")
public class ShareOrderController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(ShareOrderController.class);
    @Resource
    private ShareOrderService shareOrderService;
    @Resource
    private ItemService itemService;

    private ShareOrder shareOrder;

    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject jsonList(ShareOrder shareOrder, Page page) throws MalformedURLException, UnsupportedEncodingException {
        PageInfo<ShareOrder> pageInfo = shareOrderService.pageInfo(shareOrder, page.getLimit(), page.getPage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("isLastPage", pageInfo.isIsLastPage());
        jsonObject.put("count", pageInfo.getTotal());
        if (pageInfo != null && pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for (ShareOrder item : pageInfo.getList()) {
                row = new JSONObject();
                row.put("id", item.getId());
                row.put("memberId", item.getMemberId());
                row.put("activityId", item.getActivityId());
                row.put("activityTitle", item.getActivityTile());
                row.put("periodsNum", item.getPeriodsNum());
                row.put("itemTitle", item.getItemTitle());
                // 使用昵称
                if (StringUtil.isNotEmpty(item.getMemberName())) {
                    row.put("memberName", BaseUtil.decodeBase64(item.getMemberName()));
                }
                row.put("memberHeadImage", item.getMemberHeadImage());
                row.put("content", item.getContent());
                row.put("image", item.getImage());
                row.put("createtime", DateUtil.DateToStr(item.getCreateTime(),DateUtil.DATE_TIME_PATTERN));
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }

    /**
     * 分享订单
     */
    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject updateOrAddEntity(ShareOrderInfo shareOrderInfo) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        // 判断是否已评价
        ShareOrder record = new ShareOrder();
        record.setMemberId(shareOrderInfo.getMemberId());
        record.setActivityId(shareOrderInfo.getActivityId());
        int count = shareOrderService.selectCount(record);
        if (count >= 1) {
            returnInfo = new ReturnInfo(BaseConstant.FAIL, "您已对该次活动进行过评价");
        } else {
            shareOrder = new ShareOrder();
            shareOrder.setId(BaseUtil.uId());
            shareOrder.setMemberId(shareOrderInfo.getMemberId());
            shareOrder.setActivityId(shareOrderInfo.getActivityId());
            shareOrder.setContent(shareOrderInfo.getContent());
            shareOrder.setImage(shareOrderInfo.getImage());
            shareOrder.setCreateTime(new Date());
            int id = shareOrderService.insert(shareOrder);
            if (id <= 0) {
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
            }
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }
}
